using System;
using System.Collections.Generic;
using Csla;
using ENT.GP.Datos.MysqlHelper;
using System.Data;

namespace ENT.GP.Core
{
    [Serializable]
    public class CodigoCatalogos :
      ReadOnlyListBase<CodigoCatalogos, CodigoCatalogo>
    {
        #region Authorization Rules

        private static void AddObjectAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowGet(typeof(CodigoCatalogos), "Role");
        }

        #endregion

        #region Factory Methods

        public static CodigoCatalogos GetCodigoCatalogos()
        {
            return DataPortal.Fetch<CodigoCatalogos>();
        }

        private CodigoCatalogos()
        { /* require use of factory methods */ }

        #endregion


        #region Data Access

        private void DataPortal_Fetch()
        {
            RaiseListChangedEvents = false;
            IsReadOnly = false;

            HelperMySql objhm = HelperMySql.ObtenerConexion("");
            foreach (DataRow items in objhm.EjecutarStoreProcedure(null, "ListarCodigoCatalogos").Tables[0].Rows)
            {
                this.Add(CodigoCatalogo.GetCodigoCatalogo(items));
            }

            IsReadOnly = true;
            RaiseListChangedEvents = true;
        }

        #endregion
    }


    [Serializable]
    public class CodigoCatalogo : ReadOnlyBase<CodigoCatalogo>
    {
        #region Business Methods

        public int Id { get; set; }

        public string Codigo { get; set; }

        public string Descripcion { get; set; }

        #endregion

        #region Factory Methods

        internal static CodigoCatalogo GetCodigoCatalogo(DataRow childData)
        {
            return DataPortal.FetchChild<CodigoCatalogo>(childData);
        }

        private CodigoCatalogo()
        { /* require use of factory methods */ }

        #endregion

        #region Data Access

        private void Child_Fetch(DataRow childData)
        {
            int id;
            int.TryParse(childData[0].ToString(), out id);
            Id = id;            
            this.Codigo = childData[1].ToString();
            this.Descripcion = childData[2].ToString();
        }

        #endregion
    }
}
